package com.android.nearshop.app.adapter.listadapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.MyApplication;
import com.android.nearshop.app.widget.LoadableView.IdisplayNetErrAndRefreshView;
import com.android.nearshop.io.protocol.ProtocolManager;
import com.android.nearshop.io.protocol.in.CommentListAction;
import com.android.nearshop.io.protocol.in.SoapAction.ActionListener;
import com.android.nearshop.io.protocol.out.Comment;
import com.android.nearshop.io.protocol.out.CommentList;
import com.android.nearshop.io.protocol.out.PageInfo;
import com.android.nearshop.util.Constant;


/**
 * @File: CommontListAdapter.java
 * @Description: 查询评论列表公用适配器，此包括按序排序以及载入视图等！
 * @Author: huangyue
 * @Date: 2012-8-18 
 * @Copyright : copyrights reserved by personal 2007-2011
 */
public class CommontListAdapter extends LoadableListAdapter<Comment> implements DataLoaderHandler<Comment>{
	private CommentListAction mCommentListAction;
	 // 分页区域
    private PageInfo mPageInfo;    
    public final static int[] souce = {R.drawable.rate_good, R.drawable.rate_normal, R.drawable.rate_bad};
    private IdisplayNetErrAndRefreshView mNetView;
    //add by huangyue 当数据加载完成后给外部一个callback
    private Handler mCallback;
    private int type;
    
    public void setType(int type) {
		this.type = type;
	}

	public static final int DETAIL_TYPE = 0;
    public static final int COMMENT_TYPE = 1;
    
    public void setCallback(Handler callback) {
		this.mCallback = callback;
	}
    //end

	public CommontListAdapter(Context context, ListView listView) {
		super(context, listView, null);
		setDataLoaderHandler(this);
	}
	
	/** 设置网络异常窗口监听者*/
    public void setIdisplayNetErrAndRefreshView(IdisplayNetErrAndRefreshView view)
    {
        this.mNetView = view;
    }
    
    /**
     * 这个由外部控制,第一次载入时
     * @param action
     * @param loadingView 包含 activity_findshop布局的可以实例话这个对象
     */
    public void setAction(CommentListAction action)
    {
        mCommentListAction = action;
        mCommentListAction.setActionListener(new ActionListener<CommentList>()
                {
                    @Override
                    public void onSucceed(CommentList result)
                    {
                        if (result != null)
                        {
                            if(mPageInfo == null)
                            {
                                mPageInfo = result.pageInfo;
                                mPageInfo.curPage = mCommentListAction.curPage;
                                mPageInfo.pageNum = mCommentListAction.pageNum;
                            }
                            onLoaded(result.commentList);
                        } else
                        {
                        	//add by huangyue 找不到评论时
                        	if(mCallback != null){
                            	mCallback.hasMessages(Constant.HANDLER_NO_RESULT);
                            }
                        	//end
                            onLoaded(null);
                        }
                    }

                    @Override
                    public void onError(int resultCode)
                    {
                        if(mPageInfo != null)
                        {
                            mPageInfo.restoreNext();
                        }
                        CommontListAdapter.super.onError();
                    }
                });
    }
    
    /**
     * 从服务器下载数据
     */
    public void loadData()
    {
        showLoading();
        getNext(this);
    }
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(type == DETAIL_TYPE){
//			convertView = detailView(position, convertView);
		} else if(type == COMMENT_TYPE){
			convertView = commentView(position, convertView);
		}
		
		return convertView;
	}
	
	private View commentView(int position, View convertView) {
		if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.list_item_commont_detail, null);
        }
		
		Comment comment = (Comment) getItem(position);
		TextView textView = (TextView) convertView.findViewById(R.id.cmm_title);
		
		if(comment.cmmTitle == null || comment.cmmTitle.length() == 0){
			textView.setText(R.string.commont_no_name);
		} else {
			textView.setText(comment.cmmTitle);
		}
		
		textView = (TextView) convertView.findViewById(R.id.cmm_time);
		textView.setText(comment.cmmTime);
		
		textView = (TextView) convertView.findViewById(R.id.cmm_content);
		textView.setText(comment.cmmContent);
		
		ImageView imagetView = (ImageView) convertView.findViewById(R.id.comment_level_icon);
		imagetView.setImageResource(souce[comment.score]);
		
		return convertView;
	}

//	private View detailView(int position, View convertView) {
//		if (convertView == null)
//        {
//            convertView = inflater.inflate(R.layout.list_item_detail_comment, null);
//        }
//		
//		Comment comment = (Comment) getItem(position);
//		TextView textView = (TextView) convertView.findViewById(R.id.comment_name);
//		if(comment.cmmTitle == null || comment.cmmTitle.length() == 0){
//			textView.setText(R.string.commont_no_name);
//		} else {
//			textView.setText(comment.cmmTitle);
//		}
//		
//		textView = (TextView) convertView.findViewById(R.id.comment_text);
//		textView.setText(comment.cmmContent);
//		
//		ImageView imagetView = (ImageView) convertView.findViewById(R.id.comment_level_icon);
//		imagetView.setImageResource(souce[comment.score]);
//		return convertView;
//	}

	@Override
	public int getMaxItems() {
		// TODO Auto-generated method stub
		return 100;
	}

	@Override
	public void getNext(DataLoadedCallback<Comment> callback) 
	{
	    if(mNetView != null && !MyApplication.getContext().isNetWorkAvailable())
        {
            mNetView.showNoNetworkDialog();
        }else
        {
    		if(mPageInfo != null)
            {
                mCommentListAction.setPage(mPageInfo.getNext(), mPageInfo.pageNum);
            }
            ProtocolManager.getProtocolManager().submitAction(mCommentListAction);
        }
	}

	@Override
	public boolean isLoading() {
		// TODO Auto-generated method stub
		return mCommentListAction.isLoading();
	}

	@Override
	public boolean haveNext() {
		// TODO Auto-generated method stub
		return mPageInfo != null ? mPageInfo.hasNext() : true;
	}

}
